package com.yao.club.controller;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.request.CommentLikeRequestEntity;
import com.yao.club.entity.request.CommentRequestEntity;
import com.yao.club.service.PostsCommentService;
import com.yao.club.token.PassToken;
import com.yao.club.token.UserToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/postsComment")
@Api(value = "俱乐部帖子-帖子评论接口", description = "俱乐部帖子-帖子评论接口", tags = "俱乐部帖子-帖子评论模块")
public class PostsCommentController {

    @Autowired
    PostsCommentService postsCommentService;
    @Resource
    HttpServletRequest request;

    //https://iovapicyclone.zonsenmotor.com/club-moto/app/postsInfo/pageComment?postsId=1905528400386129921&pageNum=1&pageSize=10
    @PassToken
    @ApiOperation(value = "帖子评论", notes = "帖子评论", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true),
            @ApiImplicitParam(dataType = "Int", name = "pageNum", value = "页数", required = true),
            @ApiImplicitParam(dataType = "Int", name = "pageSize", value = "个数", required = true)
    })
    @GetMapping("/pageComment")
    public ResponseEntity<ResultEntity> pageComment(String postsId, int pageNum, int pageSize){
        return postsCommentService.pageComment(postsId, pageNum, pageSize);
    }


    @UserToken
    @ApiOperation(value = "发布评论", notes = "发布评论或回复评论", httpMethod = "POST")
    @PostMapping("/addComment")
    public ResponseEntity<Boolean> addComment(@RequestBody
                                                  @ApiParam(value = "评论实体类", name = "entity", required = true)
                                                  CommentRequestEntity entity) {
        String username = (String) request.getAttribute("username");
// 参数校验
        if (StringUtils.isEmpty(entity.getPostsId())) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        if (StringUtils.isEmpty(entity.getContent())) {
            return ResponseUtils.failed("评论内容不能为空");
        }
        if (entity.getContent().length() > 500) {
            return ResponseUtils.failed("评论内容不能超过500字");
        }

        // 区分是评论还是回复
        if (StringUtils.isEmpty(entity.getParentId())) {
            // 发布新评论
            return postsCommentService.addComment(entity,username);
        } else {
            // 回复评论
            return postsCommentService.replyComment(entity,username);
        }

    }

    @UserToken
    @ApiOperation(value = "点赞评论", notes = "点赞评论", httpMethod = "POST")
    @PostMapping("/giveLike")
    public ResponseEntity<Boolean> giveLike(@RequestBody
                                              @ApiParam(value = "点赞评论", name = "entity", required = true)
                                            CommentLikeRequestEntity entity) {
        String username = (String) request.getAttribute("username");
// 参数校验
        if (StringUtils.isEmpty(entity.getPostsId())) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        if (StringUtils.isEmpty(entity.getCommentId())) {
            return ResponseUtils.failed("评论ID不能为空");
        }

        return postsCommentService.giveLike(entity,username);

    }

    @UserToken
    @ApiOperation(value = "取消点赞评论", notes = "取消点赞评论", httpMethod = "POST")
    @PostMapping("/giveUnLike")
    public ResponseEntity<Boolean> giveUnLike(@RequestBody
                                            @ApiParam(value = "取消点赞评论", name = "entity", required = true)
                                            CommentLikeRequestEntity entity) {
        String username = (String) request.getAttribute("username");
// 参数校验
        if (StringUtils.isEmpty(entity.getPostsId())) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        if (StringUtils.isEmpty(entity.getCommentId())) {
            return ResponseUtils.failed("评论ID不能为空");
        }

        return postsCommentService.giveLikeNo(entity,username);

    }

    @UserToken
    @ApiOperation(value = "删除帖子", notes = "删除帖子", httpMethod = "GET")
    @GetMapping("/deleteComment")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "commentId", value = "评论id", required = true),
    })
    public ResponseEntity<Boolean> deleteComment(String commentId) {
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(commentId)) {
            return ResponseUtils.failed("评论ID不能为空");
        }

        return postsCommentService.deleteComment(commentId,username);

    }



}
